package com.jae.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {


	/**
	 * 昨日 00:00:00:000
	 */
	public static Date getYestoday() {
		// 时间范围
		Date startDate = new Date();
		GregorianCalendar startTime = new GregorianCalendar();
		startTime.setTime(startDate);
		startTime.add(Calendar.DAY_OF_YEAR, -1);
		startTime.set(Calendar.HOUR_OF_DAY, 0);
		startTime.set(Calendar.MINUTE, 0);
		startTime.set(Calendar.SECOND, 0);
		startTime.set(Calendar.MILLISECOND, 0);
		return startTime.getTime();
	}

	/**
	 * 昨日的时间范围 [昨日的00:00:00:000,今日的00:00:00:000)
	 * 
	 * @return {'startTime':java.util.Date,'endTime':java.util.Date}
	 */
	public static Map<String,Object> getYestodayTimeRange() {
		// 时间范围
		Date startDate = new Date();
		GregorianCalendar startTime = new GregorianCalendar();
		startTime.setTime(startDate);
		startTime.add(Calendar.DAY_OF_YEAR, -1);
		startTime.set(Calendar.HOUR_OF_DAY, 0);
		startTime.set(Calendar.HOUR_OF_DAY, 0);
		startTime.set(Calendar.MINUTE, 0);
		startTime.set(Calendar.SECOND, 0);
		startTime.set(Calendar.MILLISECOND, 0);

		Date endDate = new Date();
		GregorianCalendar endTime = new GregorianCalendar();
		endTime.setTime(endDate);
		endTime.set(Calendar.HOUR_OF_DAY, 0);
		endTime.set(Calendar.MINUTE, 0);
		endTime.set(Calendar.SECOND, 0);
		endTime.set(Calendar.MILLISECOND, 0);

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("startTime", startTime.getTime());
		map.put("endTime", endTime.getTime());

		return map;
	}

	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";
	/** 时间格式(yyyyMMdd) */
	public final static String DATE_PATTERN2 = "yyyyMMdd";
	/** 时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public static String format(Date date) {
		return format(date, DATE_PATTERN);
	}

	public static String format(Date date, String pattern) {
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			return df.format(date);
		}
		return null;
	}

	public static String getDateString(Date date) {
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return date == null ? "" : f.format(date);
	}

	/**
	 * 'yyyy-MM-dd'->Date
	 * 
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public static Date toDateYMD(String dateString) throws ParseException {
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		return f.parse(dateString);
	}

	/**
	 * 'yyyy/MM/dd'->Date
	 * 
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public static Date toDateYMD2(String dateString) throws ParseException {
		SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
		return f.parse(dateString);
	}

	public static String getYearMonthDay(Date date) {
		SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
		return date == null ? "" : f.format(date);
	}

	public static Date toDateYMDHMS(String dateString) throws ParseException {
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return f.parse(dateString);
	}

	/**
	 * 根据日期字符串返回时间范围 并封装到map
	 * 
	 * @param created_date
	 * @param map
	 * @throws Exception
	 */
	public static void getCreatedateArea(String created_date, Map<String,Object> map) throws Exception {

		if (created_date != null && !created_date.equals("")) {
			created_date = created_date.trim();
			if (created_date.contains("-")) {
				// 日期范围
				String[] r = created_date.split("-");
				// 开始日期 00:00:00:000
				String created_date_from_str = r[0].trim();
				Date created_date_from = DateUtil.toDateYMD2(created_date_from_str);
				map.put("created_date_from", created_date_from);

				// 结束日期 23:59:59:999
				String created_date_to_str = r[1].trim();
				Date created_date_to = DateUtil.toDateYMD2(created_date_to_str);
				GregorianCalendar c = new GregorianCalendar();
				c.setTime(created_date_to);
				c.add(Calendar.DAY_OF_YEAR, 1);
				c.add(Calendar.MILLISECOND, -1);
				created_date_to = c.getTime();
				map.put("created_date_to", created_date_to);

			} else {
				// 单个日期
				created_date = created_date.trim();
				// 开始日期 00:00:00:000
				Date created_date_from = DateUtil.toDateYMD2(created_date);
				map.put("created_date_from", created_date_from);

				// 结束日期 23:59:59:999
				Date created_date_to = DateUtil.toDateYMD2(created_date);
				GregorianCalendar c = new GregorianCalendar();
				c.setTime(created_date_to);
				c.add(Calendar.DAY_OF_YEAR, 1);
				c.add(Calendar.MILLISECOND, -1);
				created_date_to = c.getTime();
				map.put("created_date_to", created_date_to);
			}
		}

	}

	public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static String toDateTime(Date date) {
		return toDateText(date, DEFAULT_DATE_TIME_FORMAT);
	}

	public static String toDateText(Date date, String pattern) {
		if (date == null || pattern == null) {
			return null;
		}
		SimpleDateFormat dateFormat = newDateFormat(pattern);
		return dateFormat.format(date);
	}

	private static SimpleDateFormat newDateFormat(String pattern) {
		return new SimpleDateFormat(pattern, Locale.SIMPLIFIED_CHINESE);
	}

	public static void main(String[] args) {
//		long s = 1529589670000L;
//
//		Date d = new Date(s);
//		System.out.println(getDateString(d));

		Date date = new Date();
		Date newdate = DateUtil.addMinute(date, 29);
		System.out.println(DateUtil.toDateTime(newdate));

	}

	/**
	 * 时分秒清零
	 * 
	 * @param cal
	 * @return
	 */
	public static GregorianCalendar setHmsSZero(GregorianCalendar cal) {
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal;
	}

	/**
	 * 处理从前端Daterangpicker插件传过来的日期范围
	 * 
	 * @param dateRange
	 * @param searck_key
	 * @param map
	 * @throws ParseException
	 */
	public static void handleDaterangpickerData(String dateRange, String searck_key, Map<String,Object> map)
			throws ParseException {
		if (dateRange != null && !dateRange.equals("")) {
			dateRange = dateRange.trim();
			if (dateRange.contains("-")) {
				// 日期范围
				String[] r = dateRange.split("-");
				// 开始日期 00:00:00:000
				String from_str = r[0].trim();
				Date date_from = DateUtil.toDateYMD2(from_str);
				map.put(searck_key + "_from", date_from);

				// 结束日期 23:59:59:999
				String to_str = r[1].trim();
				Date date_to = DateUtil.toDateYMD2(to_str);
				GregorianCalendar c = new GregorianCalendar();
				c.setTime(date_to);
				c.add(Calendar.DAY_OF_YEAR, 1);
				c.add(Calendar.MILLISECOND, -1);
				date_to = c.getTime();
				map.put(searck_key + "_to", date_to);

			} else {
				// 单个日期
				dateRange = dateRange.trim();
				// 开始日期 00:00:00:000
				Date date_from = DateUtil.toDateYMD2(dateRange);
				map.put(searck_key + "_from", date_from);

				// 结束日期 23:59:59:999
				Date date_to = DateUtil.toDateYMD2(dateRange);
				GregorianCalendar c = new GregorianCalendar();
				c.setTime(date_to);
				c.add(Calendar.DAY_OF_YEAR, 1);
				c.add(Calendar.MILLISECOND, -1);
				date_to = c.getTime();
				map.put(searck_key + "_to", date_to);
			}
		}
	}

	/**
	 * 时间添加分钟
	 * 
	 * @param minute 分钟数
	 * @return
	 */
	public static Date addMinute(Date date, long minute) {
		long ss = minute * 60 * 1000;
		return new Date(date.getTime() + ss);
	}

	/**
	 * 获取时间差（分钟）
	 * 
	 * @param endDate
	 * @param nowDate
	 * @return
	 */
	public static long getDateDiffOfMinute(Date nowDate, Date endDate) {
		long nm = 1000 * 60;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少分钟
		long min = diff / nm;
		return min;
	}

	/**
	 * 获取时间差（小时）
	 * 
	 * @param endDate
	 * @param nowDate
	 * @return
	 */
	public static long getDateDiffOfHour(Date nowDate, Date endDate) {
		long nh = 1000 * 60 * 60;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少小时
		long min = diff / nh;
		return min;
	}

	/**
	 * 把当前日期往后增加一年.整数往后推,负数往前移动
	 * 
	 * @param year
	 * @return
	 */
	public static Date addYear(int year) {
		Calendar calendar = new GregorianCalendar();
		Date date = new Date();
		calendar.setTime(date);
		calendar.add(calendar.YEAR, year);// 把日期往后增加一年.整数往后推,负数往前移动
//		    calendar.add(calendar.DAY_OF_MONTH, 1);//把日期往后增加一个月.整数往后推,负数往前移动
//		    calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 
//		    calendar.add(calendar.WEEK_OF_MONTH, 1);//把日期往后增加一个月.整数往后推,负数往前移动
//		    date=calendar.getTime();   //这个时间就是日期往后推一天的结果 
		return calendar.getTime();
	}

	/**
	 * 把当前日期往后增加一天.整数往后推,负数往前移动
	 * 
	 * @param year
	 * @return
	 */
	public static Date addDay(int days) {
		Calendar calendar = new GregorianCalendar();
		Date date = new Date();
		calendar.setTime(date);
		calendar.add(calendar.DATE, days);// 把日期往后增加一天.整数往后推,负数往前移动
		return calendar.getTime();
	}
	
	/**
	 * 把当前日期往后增加一个小时.整数往后推,负数往前移动
	 * 
	 * @param year
	 * @return
	 */
	public static Date addHour(Date date, int hour) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.HOUR, hour);// 把日期往后增加一年.整数往后推,负数往前移动
		return calendar.getTime();
	}
	
	/**
	 * 得到当前日期的开始时间
	 * @return
	 */
	public static Date getBeginOfCurrentDay() {
	    return getBeginOfTheDate(new Date());
	}

	/**
	 * 得到当前日期的结束时间
	 * @return
	 */
	public static Date getEndOfCurrentDay() {
	    return getEndOfTheDate(new Date());
	}
	
	/**
	 * 获取当日的起始时间
	 * @param date
	 * @return
	 */
	public static Date getBeginOfTheDate(Date date) {
	    Calendar nowday = Calendar.getInstance();
	    nowday.setTime(date);
	    nowday.set(Calendar.HOUR_OF_DAY, 0);
	    nowday.set(Calendar.MINUTE, 0);
	    nowday.set(Calendar.SECOND, 0);
	    nowday.set(Calendar.MILLISECOND, 0);
	    return nowday.getTime();
	}


	/**
	 * 获取当日结束时间
	 * @param date
	 * @return
	 */
	public static Date getEndOfTheDate(Date date) {
	    Calendar nowday = Calendar.getInstance();
	    nowday.setTime(date);
	    nowday.set(Calendar.HOUR_OF_DAY, 23);
	    nowday.set(Calendar.MINUTE, 59);
	    nowday.set(Calendar.SECOND, 59);
	    nowday.set(Calendar.MILLISECOND, 998);//Sql Server BUG
	    return nowday.getTime();
	}

}
